const initState = {
    yesList: JSON.parse(localStorage.getItem('yesList')) || [],
    noList: JSON.parse(localStorage.getItem('noList')) || []
}
export function todoReduer(state = initState, action) {
    // console.log(action);
    switch (action.type) {
        // 讲input中的值添加到未完成
        case "addtodo":
            var isShow = false;
            state.noList.map((ele) => {
                console.log(ele);
                if (ele.text === action.text) {
                    alert('不能添加相同todo');
                    isShow = true
                }
            })
            if (!isShow) {
                var todo = {
                    text: action.text,
                    satus: "WEIWANC"
                }
                state.noList.push(todo)
            }
            localStorage.noList = JSON.stringify(state.noList)
            localStorage.yesList = JSON.stringify(state.yesList)
            return {
                noList: [...state.noList],
                yesList: state.yesList
            }
        case "addyeslist":
            state.noList.forEach((ele, index) => {
                // console.log(ele)
                if (ele.text === action.text.text) {
                    state.noList.splice(index, 1)
                }
            })
            // 将已经完成中的satus改变
            action.text.satus = "FINISHED"
            state.yesList.push(action.text)
            localStorage.noList = JSON.stringify(state.noList)
            localStorage.yesList = JSON.stringify(state.yesList)
            return {
                noList: [...state.noList],
                yesList: [...state.yesList]
            }
        case "addnolist":

            state.yesList.map((ele,index) => {
                // console.log(action.text.text);
                if (ele.text === action.text.text) {
                    state.yesList.splice(index, 1)
                }
            })
            action.text.satus = "WEIWANC"
            state.noList.push(action.text)
            localStorage.noList = JSON.stringify(state.noList)
            localStorage.yesList = JSON.stringify(state.yesList)
            return {
                noList: [...state.noList],
                yesList: [...state.yesList]
            }

    }
    return state
}